package com.yd.app.mapper.provider;

import com.alibaba.druid.util.StringUtils;
import com.yd.domain.business.Coupon;

import java.util.Date;
import java.util.Map;

/**
 * 优惠券
 * Created by sunxianhui on 2018/3/7.
 */
public class CouponProvider extends BaseProvider {
    /**
     * 获取每一页列表
     * @param map
     * @return
     */
    public String searchCouponList(Map map) {
        Coupon coupon = (Coupon) map.get("coupon");
        StringBuilder sb = new StringBuilder("SELECT * FROM business_coupon where 1=1 ");
        sb.append(searchOtherSql(coupon));
        sb.append(" limit #{startRow},#{pageSize} ");
        return sb.toString();
    }

    /**
     * 获取查询总数量
     * @param map
     * @return
     */
    public String searchCouponCount(Map map) {
        Coupon coupon = (Coupon) map.get("coupon");
        StringBuilder sb = new StringBuilder(" select count(1) from business_coupon where 1=1");
        sb.append(searchOtherSql(coupon));
        return sb.toString();
    }

    /**
     * 拼装查询条件
     * @param coupon
     * @return
     */
    public String searchOtherSql(Coupon coupon) {
        StringBuilder sb = new StringBuilder();
        if(!StringUtils.isEmpty(coupon.getTitle())&&!"".equals(coupon.getTitle())){
            sb.append(" and title like" +" '%"+coupon.getTitle().trim()+"%'  ");
        }
        if(coupon.getDealPrice()>0){
            sb.append(" and dealPrice ='"+coupon.getDealPrice()+"'  ");
        }
        if(coupon.getDiscountPrice()>0){
            sb.append(" and discountPrice ='"+coupon.getDiscountPrice()+"'  ");
        }
        if(coupon.isIfCanUse()){
            sb.append(" and endDate >"+new Date().getTime()+" ");
        }else{
            sb.append(" and endDate <"+new Date().getTime()+"  ");
        }
        return sb.toString();
    }
}
